<?php
/**
 * Created by PhpStorm.
 * User: xiong <1099446868@qq.com>
 * DateTime: 2018/12/17
 * TIME: 14:59
 */

namespace app\index\model;


use think\Db;
use think\facade\Cache;

class Index extends BaseModel {

    public static function getIndex($source) {

        //获取category
        $return['category'] = self::getCategory($source);
        //最近更新
        //$return['lastUpdate'] = self::getLastUpdate($source);
        //首页中心推荐
        //$return['indexRecommend'] = self::getIndexRecommend();  //采集不如自己写推荐
        $return['indexRecommend'] = [
            'topleft'=>[
                0=>[1=>'以宋为图，量河为疆：宋疆',3=>'宋疆'],
            ],
            'topright'=>[
                0=>[1=>'极品庶子：穿成庶子，悠闲古代生涯',3=>'极品庶子'],
            ],
            'left'=>[],
            'right'=>[]
        ];
        //各种榜单
        $return['rankingList'] = self::getRankingList(1);
        return $return;
    }

    public static function getCategory($source){
        if(Cache::has('category_'.$source)){
            $category = Cache::get('category_'.$source);
        }else{
            $category = Db::name('category')->where('source', $source)->select();
            Cache::set('category_'.$source, $category, config('category_time'));
        }
        return $category;
    }

    public static function getLastUpdate(){
        if (Cache::has('LastUpdate')) {

            $getLastUpdate = Cache::get('LastUpdate');

        } else {
            $getLastUpdate = self::LastUpdate();
            Cache::set('LastUpdate',$getLastUpdate, config('LastUpdate_time'));
        }
        return $getLastUpdate;
    }

    public static function getRankingList($page){
        if (Cache::has('RankingListFromzh')) {
            $rankingList = Cache::get('RankingListFromzh');
        } else {
            $rankingList = self::RankingList($page);
            Cache::set('RankingListFromzh',$rankingList, config('RankingList_time'));
        }

        return $rankingList;
    }

    public static function getIndexRecommend(){
        if (Cache::has('IndexRecommend')) {
            $indexRecommend = Cache::get('IndexRecommend');
        } else {
            $indexRecommend = self::IndexRecommend();
            Cache::set('IndexRecommend',$indexRecommend, config('RankingList_time'));
        }
        return $indexRecommend;
    }

    public static function LastUpdate(){
        $xpath = config('xpath');
        $url = $xpath['zh']['lastupdate']['url'];
        $html = curl_get2($url);
        $html = json_decode($html, true);
        return $html;
    }


    /**首页中心推荐 left|right  1是显示描述 2是bookname
     * @return array
     */
    public static function IndexRecommend(){
        $xpath = config('xpath');
        $selects = $xpath['zh']['index']['select'];
        $url = $xpath['zh']['index']['url'];
        $html = curl_get($url);

        $r = [];
        foreach ($selects as $z => $v) {
            $select = $v[0];
            $set = $v[1];
            $con = getContent($html, $select, true);
            $con = trim($con);
            $con = explode("\n",$con);
            $sort = 0;
            foreach($con as $a=>$s){
                if($a%2 !== 0){
                    unset($con[$a]);
                }else{
                    $s = trim($s);
                    unset($con[$a]);
                    $self = $s;
                    $s = mb_substr($s, 4);
                    $con[$sort][0] = $s;
                    $con[$sort][1] = $self;
                    $con[$sort][2] = mb_substr($self, 0, 4);
                    $sort++;
                }
            }
            foreach($con as $d=>$f){
                if($set[$d] !== -1){
                    dump($con);exit;
                    $title = explode('：', $f[0]);
                    dump($d);
                    dump($set);
                    dump($title);exit;
                    $title = $title[$set[$d]];
                    $f[3] = $title;
                }
                $con[$d] = $f;
            }
            $r[$z] = $con;
        }
        return $r;
    }

    /** 获取推荐榜
     * @param int $page 分页   zh每页20条
     * @return array
     */
    public static function RankingList(int $page = 1):array {

        $xpath = config('xpath');
        $selects = $xpath['zh']['list'];
        $r = [];
        foreach ($selects as $z => $v) {
            $url = $v['url'] . '&p=' . $page;
            $html = curl_get($url);
            $con = getContent($html, $v['select'], true);
            $con = trim($con);
            $con = explode("加入书架", $con);
            foreach ($con as $k => $v) {
                $v = trim($v);
                $con[$k] = explode("\n", $v);
                foreach ($con[$k] as $n => $m) {
                    $con[$k][$n] = trim($m);
                }
                if ($k < 20) {
                    $r[$z][$k]['bookname'] = $con[$k][0];
                    $author = explode('|', $con[$k][3]);
                    $r[$z][$k]['author'] = $author[0];
                }
            }
        }

        return $r;
    }
}